package cn.edu.abtu.controller;

import cn.edu.abtu.entity.SysDepartment;
import cn.edu.abtu.service.SysDepartmentService;
import cn.edu.abtu.utils.AjaxResult;
import cn.edu.abtu.vo.query.DepartmentQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/sysDepartment")
public class SysDepartmentController {

    /**
     * 服务对象
     */
    @Resource
    private SysDepartmentService sysDepartmentService;

    @GetMapping("/list")
    public AjaxResult list(DepartmentQueryVo departmentQueryVo){
        List<SysDepartment> departmentList = sysDepartmentService.getDepartmentList(departmentQueryVo);
        return AjaxResult.success("查询成功!",departmentList);
    }

    @GetMapping("/parent/list")
    public AjaxResult getParentDepartment(){
        List<SysDepartment> parentDepartment = sysDepartmentService.getParentDepartment();
        return AjaxResult.success("查询成功!",parentDepartment);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysDepartment sysDepartment){
        sysDepartment.setIsDelete(0);
        boolean result = sysDepartmentService.save(sysDepartment);
        if (result){
            return AjaxResult.success("部门信息添加成功!",null);
        }
        return AjaxResult.error("部门信息添加失败!",null);
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody SysDepartment sysDepartment){
        boolean result = sysDepartmentService.updateById(sysDepartment);
        if (result){
            return AjaxResult.success("部门信息修改成功!",null);
        }
        return AjaxResult.error("部门信息修改失败!",null);
    }


    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        return sysDepartmentService.removeById(id);
    }
}

